package mybatisgen.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: com.xinmei.mybatisgen.plugin.ColumnCommentGenerator
 * @Description: 生成列的备注
 * @author gdw
 * @date 2017年6月21日 上午10:37:56
 */
public class ColumnCommentGenerator extends DefaultCommentGenerator {
	private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
	@Override
	public void addFieldComment(Field field, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
		if (introspectedColumn.getRemarks() == null
				|| introspectedColumn.getRemarks().length() == 0){
			return ;
		}
		
		field.addJavaDocLine("/**");
		for(String remark : introspectedColumn.getRemarks().split("\n")){
			field.addJavaDocLine(" * " + remark);
		}
		field.addJavaDocLine(" */");
	}

	@Override
	public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		// 获取表注释
		String remarks = introspectedTable.getRemarks();
		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * " + remarks);
		topLevelClass.addJavaDocLine(" * @author mybatis generator");
		topLevelClass.addJavaDocLine(" * @since " + FORMATTER.format(LocalDateTime.now()));
		topLevelClass.addJavaDocLine(" */");
	}
}
